package ru.st.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import ru.st.selenium.model.Film;

public class InternalPage extends AnyPage {

	public InternalPage(PageManager pages) {
		super(pages);
	}

	public InternalPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By.cssSelector("nav")));
		wait.until(presenceOfElementLocated(By.cssSelector("footer")));
		return this;
	}

	public InternalPage ensureMainPageLoaded() {
		ensurePageLoaded();
		wait.until(presenceOfElementLocated(By
				.xpath("//div[@id='results']/a//div[@class='title']")));
		return this;
	}

	public InternalPage ensureThereAreNoSearchResults() {
		ensurePageLoaded();
		wait.until(presenceOfElementLocated(By
				.xpath("//div[@id='results']/div[@class='content']")));
		return this;
	}

	@FindBy(css = "h1")
	private WebElement header;

	@FindBy(css = "nav a[href $= '?go=profile']")
	private WebElement userProfileLink;

	@FindBy(css = "nav a[href $= '?go=users']")
	private WebElement userManagementLink;

	@FindBy(xpath = "//*[contains (@href, 'add')]")
	private WebElement addMovieLink;

	@FindBy(id = "q")
	private WebElement searchBar;

	@FindBy(xpath = "//*[contains (@href, 'logout')]")
	private WebElement logoutLink;

	public InternalPage clickHeader() {
		header.click();
		return pages.internalPage;
	}

	public UserProfilePage clickUserProfilePage() {
		userProfileLink.click();
		return pages.userProfilePage;
	}

	public UserManagementPage clickUserManagementLink() {
		userManagementLink.click();
		return pages.userManagementPage;
	}

	public MoviePage clickAddMovieLink() {
		addMovieLink.click();
		return pages.addMoviePage;
	}

	public LoginPage clickLogoutLink() {
		logoutLink.click();
		wait.until(alertIsPresent()).accept();
		return pages.loginPage;
	}

	public MoviePage gotoMovie(Film film) {
		WebElement createdMovie = driver.findElement(By
				.xpath("//div[@id='results']//div[@class='title'][text() = '"
						+ film.getTitle() + "']"));
		createdMovie.click();
		return pages.moviePage;
	}

	public List<Film> getMoviesList() {
		List<Film> moviesList = new ArrayList<Film>();
		List<WebElement> movies = driver.findElements(By
				.xpath("//div[@id='results']/a//div[@class='title']"));
		for (WebElement movie : movies) {
			String movieTitle = movie.getText();
			moviesList.add(new Film().setTitle(movieTitle));
		}
		return moviesList;
	}

	public void search(String title) {
		pages.internalPage.ensurePageLoaded();
		searchBar.sendKeys(Keys.chord(Keys.CONTROL, "a"), title + Keys.RETURN);
	}

	public List<Film> searchWithResults(String title) {
		search(title);
		pages.internalPage.ensureMainPageLoaded();
		return getMoviesList();
	}

	public boolean searchWithoutResults(String title) {
		search(title);
		pages.internalPage.ensureThereAreNoSearchResults();
		return (getMoviesList().size() == 0);
	}

	public boolean movieIsPresent(Film film) {
		List<Film> moviesList = getMoviesList();
		List<String> titlesList = new ArrayList<String>();
		for (Film movie : moviesList) {
			titlesList.add(movie.getTitle());
		}
		return titlesList.contains(film.getTitle());
	}
}
